const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectId: 0,
    tab1: [],
    addTab: [{
      name: '全部',
      objectSid: 0
    }],
    imgurl: '', //canting logo ip
    restaurant: [],
    hidden: 'false',
    degree: '', //抽奖机会
    jiangpin: true,
    animationData:'',
    animation:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let id = 0
    var that = this
    app.post(app.url.authorlist).then(res => {
      console.log(res)
      let addtab = that.data.addTab
      let newTab = res.data.list.concat(addtab).reverse()
      that.setData({
        tab1: newTab
      })
      that.getDining(id)
    })
  },

//获取userid
  food_details(e) {
    var id = e.currentTarget.dataset.setid
    console.log(id)
    wx.navigateTo({
      url: '../../pages/dining/dining?id=' + id,
    })
  },
  //加载餐厅信息
  getDining(id) {
    var that = this
    app.post(app.url.restaurant, {
      role_id: id
    }).then(res => {
      console.log(res)
      if (res.data.message == 0) {
        that.setData({
          messageNo: '暂无餐厅信息',
          restaurant: [],
          imgurl: [],
          hidden: 'true'
        })
        return false
      } else {
        that.setData({
          restaurant: res.data.data,
          imgurl: res.data.urls,
          hidden: 'true'
        })
      }
    }).catch(res => {
    
    })
  },
  //抽奖弹框
  getLuckSum() {
    var _this = this
    setTimeout(function() {
      _this.time()
    }, 2000)

  },
  // 请求抽奖次数
  time() {
      console.log(app.login.types)
      console.log('进来')
        var openid=app.globalDataO.openid.openid
        console.log(openid)
      app.post(app.url.userid, {openId:openid}).then(res => {
       var userid=res.data.objectSid
       console.log(userid)
        this.getverche(userid)
        wx.setStorage({
          key: 'sdfsdf'
        })
      })
    
  },
//判断是否授权
isLogin(userid){
  var that=this
  wx.getSetting({
    success: function (res) {
      if (res.authSetting['scope.userInfo']) {
        that.getLuckSum()
      } else {
      }
    },
  })
},
  getverche(userid){
    app.post(app.url.lottery.degree, {userId:userid}).then(re => {
      console.log(re.data)
      this.setData({
        degree: re.data.luckCount
      })
      this.degree()
    })
  },
  //去抽奖
  goverche() {
    this.setData({
      animation: true
    })
    wx.navigateTo({
      url: '../../pages/lottery/lottery',
    })
  },
  //判断抽奖机会
  degree() {
    if (this.data.degree >= 1) {
      this.come()
    }
  },


  //动画
  come(){
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: "ease",
      delay: 0,
    })
    animation.translateY(400, 0).step();
    this.setData({
      animationData: animation.export(),
    })

  },
  //取消抽奖框
  go(){
this.setData({
  animation:true
})
  },
  //餐厅分类选项
  selectIds(e) {
    var that = this
    var id = e.currentTarget.dataset.id
    that.setData({
      selectId: e.currentTarget.dataset.index,
      hidden:false
    })
    that.getDining(id)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.isLogin()
    app.getOpenid()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})